import React from 'react';
import ReactDOM from 'react-dom';
import logo from './logo.svg';
import Redux, { createStore } from "redux";


/**
 * 从主页面拷贝的，需要修改
 */


const renduer = function(state={num:0},action){
    switch(action.type){
      case "add":
        state.num++;
        break;
      case "jian":
        state.num--;
        break;
      default:
        break;
    }
    console.log( state.num);
    console.log("-----------")
    console.log( {...state})
    return {...state}
  }
  
  const store = createStore(renduer);//创建一个数据仓库，传进去的renduer是一个方法；
  
  function App(props) {
    let state = store.getState();
    return (
      <div className="App">
        <h1>redux的数据：{state.num}</h1>
        <button onClick={add}>加法+1</button>
        <button onClick={jianfa}>剑法-1</button>
      </div>
      
    );
  }
  
  function add(){
    store.dispatch({type:"add"})
    console.log("--")
  }
  function jianfa(){
    store.dispatch({type:"jian"})
  }
  ReactDOM.render(
      <App/>,
    document.getElementById('root')
  );
  
  store.subscribe(()=>{
    ReactDOM.render(
      <App></App>,
      document.querySelector("#root")
    )
  })

//数据改变，渲染道页面中
export default App;
